package org.dna.wickwtui.jobs;

import org.apache.log4j.Logger;
import org.apache.wicket.feedback.ContainerFeedbackMessageFilter;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.form.validation.FormComponentFeedbackBorder;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.validation.validator.MinimumValidator;
import org.apache.wicket.validation.validator.RangeValidator;
import org.dna.MetroApplication;
import org.dna.metronomo.persistence.Activity;
import org.dna.metronomo.persistence.IActivityDAO;
import org.dna.metronomo.persistence.Job;
import org.dna.uimodel.LoadableActivityModel;
import org.dna.uimodel.SelectableActivity;
import org.dna.wickwtui.MetroPage;
import org.dna.wickwtui.activities.ManageActivityPage;
import org.dna.wickwtui.utils.AtLeastOneValidator;

public class ManageJobPage extends MetroPage {
	
	private static final Logger LOG = Logger.getLogger(ManageJobPage.class);
	
	private boolean m_editMode = false;

	private Button btnSumbit;
	private TextField<String> tfName;
	private TextArea<String> taDescription;
	private TextField<Integer> tfDays;
	private TextField<Integer> tfHours;
	private TextField<Integer> tfMinutes;
	
	private IModel<Activity> m_parentActModel;
	
	//this store of unboxing in necessary to avoid users the duty to insert all 
	//time fields.
	protected Integer daysU;
	protected Integer hoursU;
	protected Integer minutesU;

	/**
	 * Not bookmarkable, use to add a job to an existing activity
	 * */
	public ManageJobPage(SelectableActivity parentActivity) {
		m_editMode = false;
		Job newJob = new Job();
		m_parentActModel = new LoadableActivityModel(parentActivity);
		initComponents(newJob);
	}

	/**
	 * Not bookmarkable, use to edit an existing job
	 * */
	public ManageJobPage(Job editable) {
		m_editMode = true;
		m_parentActModel = new LoadableActivityModel(editable.getActivity());
		initComponents(editable);
	}

	@SuppressWarnings( { "serial", "unchecked" })
	private void initComponents(final Job job) {
		Form form = new Form("form") {
			@Override
			protected void onSubmit() {
				if (daysU != null) job.getElapsed().setDays(daysU);
				if (hoursU != null) job.getElapsed().setHours(hoursU);
				if (minutesU != null) job.getElapsed().setMinutes(minutesU);
				
				IActivityDAO dao = MetroApplication.get().getActivityDAO();
				if (m_editMode) {
					dao.updateJob(job);
				} else {
					dao.addJobToActivity(job, m_parentActModel.getObject());
				}
				// navigate back to the activity details
				ManageActivityPage target = new ManageActivityPage();
				target.setCurrentActivity(m_parentActModel.getObject());
				setResponsePage(target);
			}
		};
		form.setModel(new CompoundPropertyModel(job));
		add(form);
		
		if (job.getElapsed().getDays() != 0) daysU = job.getElapsed().getDays();
		if (job.getElapsed().getHours() != 0) hoursU = job.getElapsed().getHours();
		if (job.getElapsed().getMinutes() != 0) minutesU = job.getElapsed().getMinutes();
			
		tfName = new TextField<String>("name");
		taDescription = new TextArea<String>("description");
		tfDays = new TextField<Integer>("days", new PropertyModel(this, "daysU"));
		tfDays.add(new MinimumValidator<Integer>(0));
		tfHours = new TextField<Integer>("hours", new PropertyModel(this, "hoursU"));
		tfHours.add(new RangeValidator<Integer>(0, 23));
		tfMinutes = new TextField<Integer>("minutes", new PropertyModel(this, "minutesU"));
		tfMinutes.add(new RangeValidator<Integer>(0, 59));
		String submitLabel = "Update";
		if (!m_editMode) {
			submitLabel = "Create";
			tfName.setRequired(true);
			taDescription.setRequired(true);
		}
		btnSumbit = new Button("button", new Model(submitLabel));
		form.add(new FormComponentFeedbackBorder("border").add(tfName));
		form.add(btnSumbit);
		form.add(taDescription);
		form.add(tfDays);
		form.add(tfHours);
		form.add(tfMinutes);
		form.add(new AtLeastOneValidator(tfDays, tfHours, tfMinutes));

		add(new FeedbackPanel("feedback", new ContainerFeedbackMessageFilter(form)));
		LOG.info("Init form components finished in " + (m_editMode ? "edit" : "creation") + " mode");
	}
}